package com.example.demo.Mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.Session;
import com.example.demo.vo.sessionVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SessionMapper extends BaseMapper<Session> {

    @Select("select id,title,created_at from session where user_id=#{userId} and tag=0 ORDER BY created_at DESC")
    @Results({
            @Result(property = "id", column = "id", id = true),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "title", column = "title")
    })
    List<sessionVO> getList(@Param("userId") Long userId);

    @Select("select id from session where user_id=#{userId} and tag=1")
    Long getSessionId(@Param("userId") Long userId);

    @Select("select title from session where id=#{sessionId}")
    String getSessionTitle(@Param("sessionId")Long sessionId);

    @Update("update session set title=#{title} where id=#{sessionId}")
    void updateSessionTitle(@Param("sessionId")Long sessionId,@Param("title")String title);

    @Select("select id from session where user_id=#{userId} and role_id=#{roleId}")
    Long getSessionIdByUserIdAndRoleId(@Param("userId") Long userId, @Param("roleId") Long roleId);

    /**
     * 根据sessionId获取对应角色的voice配置
     * @param sessionId 会话ID
     * @return voice配置字符串
     */
    @Select("SELECT r.voice FROM session s " +
            "INNER JOIN character r ON s.role_id = r.id " +
            "WHERE s.id = #{sessionId}")
    String getVoiceBySessionId(@Param("sessionId") Long sessionId);

    // 新增：根据sessionId获取对应角色的description属性
    /**
     * 根据sessionId获取对应角色的description
     * @param sessionId 会话ID
     * @return 角色描述字符串
     */
    @Select("SELECT r.description FROM session s " +
            "INNER JOIN character r ON s.role_id = r.id " +
            "WHERE s.id = #{sessionId}")
    String getDescriptionBySessionId(@Param("sessionId") Long sessionId);
}
